#!/usr/bin/env python
# encoding: utf-8

import re, urllib, urllib2, cookielib, codecs, chardet, sys

class LoginRenRen():
    def __init__(self, name = '', password = '', domain = ''):
        self.name = name
        self.password = password
        self.domain = domain

        self.cj = cookielib.LWPCookieJar()
        try:
            self.cj.revert('renren.cookie')
            print 'successed got a cookie..'
        except Exception, e:
            print 'Can not get the cookies',e.message

        #装载cookies
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
        urllib2.install_opener(self.opener)

    def login(self):
        params = {'domain':self.domain,'email':self.name,'password':self.password}
        req = urllib2.Request(
            'http://www.renren.com/PLogin.do',
            urllib.urlencode(params)
        )
        print 'login.....'

        self.openrate = self.opener.open(req)

        #查看下返回的url来判断登陆进去了没
        print self.openrate.geturl()
        #info = self.openrate.read()

        #查看了一下页面编码 chardet是第三方库
        #print chardet.detect(info)
        print ''
        #print re.findall(r'password',info)
        #打印出了返回的内容
        type = sys.getfilesystemencoding()
        #print info.decode("UTF-8").encode(type)


if __name__=='__main__':
    username = 'liuzhizhi123@126.com' #用户名
    password = '.....' #密码
    domain = 'renren.com'
    ren = LoginRenRen(username,password,domain)
    ren.login()


